﻿Imports Oracle.DataAccess.Client
Public Class EstadoProducto
    Private lTipo As Char
    Private lDescripcionEstado As String
    Public Sub New(ByVal mTipo As Char, ByVal Descrip As String)
        lTipo = mTipo
        lDescripcionEstado = Descrip
    End Sub
    Public Property Tipo() As Char
        Get
            Return lTipo
        End Get
        Set(ByVal value As Char)
            lTipo = value
        End Set
    End Property
    Public Property DescripcionEstado() As String
        Get
            Return lDescripcionEstado
        End Get
        Set(ByVal value As String)
            lDescripcionEstado = value
        End Set
    End Property
End Class

Public Class cMarcas
    Private lDrMarca As dsProductos.PRO_MARCASRow
    Public Sub New(ByVal idMarca As String)
        Dim daMarca As New dsProductosTableAdapters.PRO_MARCASTableAdapter
        Try
            Dim dtMarca As dsProductos.PRO_MARCASDataTable = daMarca.GetDataByIdMaraca(idMarca)
            If dtMarca.Rows.Count = 0 Then
                Throw New Exception("Marca no existe verifique e intente de nuevo")
            Else
                lDrMarca = dtMarca.Rows(0)
            End If
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        End Try
    End Sub
    Public ReadOnly Property idMarca() As String
        Get
            Return lDrMarca.IDMARCA
        End Get
    End Property
    Public ReadOnly Property DescripcionMarca() As String
        Get
            Return lDrMarca.DESCRIPCION
        End Get
    End Property
    Public ReadOnly Property Prefijo() As String
        Get
            Return lDrMarca.PREFIJO
        End Get
    End Property
    Public ReadOnly Property Dijitos() As Int16
        Get
            Return lDrMarca.DIJITOSCONSEC
        End Get
    End Property
End Class

Public Class cClaseProducto
    Private ldrClase As dsProductos.PRO_CLASERow
    Public Sub New(ByVal idClase As String)
        Dim daClase As New dsProductosTableAdapters.PRO_CLASETableAdapter
        Try
            Dim dtClas As dsProductos.PRO_CLASEDataTable = daClase.GetDataByIdClase(idClase)
            If dtClas.Rows.Count = 0 Then
                Throw New Exception("Clase no existe verifique e inetente de nuevo")
            Else
                ldrClase = dtClas.Rows(0)
            End If
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        End Try
    End Sub
    Public ReadOnly Property idClase() As String
        Get
            Return ldrClase.IDCLASE
        End Get
    End Property
    Public ReadOnly Property DescripcionClase() As String
        Get
            Return ldrClase.DESCRIPCION
        End Get
    End Property
End Class

Public Class cTipoProducto

    Private lDaTipoProd As lbGlobalcom.dsProductosTableAdapters.PRO_TIPOPRODUCTOTableAdapter
    Private ldtTipoProd As lbGlobalcom.dsProductos.PRO_TIPOPRODUCTODataTable
    Private lDrTipoProd As lbGlobalcom.dsProductos.PRO_TIPOPRODUCTORow

    Sub New(ByVal TipoProducto As String)
        lDaTipoProd = New lbGlobalcom.dsProductosTableAdapters.PRO_TIPOPRODUCTOTableAdapter
        ldtTipoProd = New lbGlobalcom.dsProductos.PRO_TIPOPRODUCTODataTable
        Try
            lDaTipoProd.FillByidTipoProd(ldtTipoProd, TipoProducto)
            If ldtTipoProd.Rows.Count > 0 Then
                lDrTipoProd = ldtTipoProd.Rows(0)
            Else
                Throw New Exception("No se econtro registro para este tipo de producto")
            End If
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        End Try

    End Sub

    Public ReadOnly Property IdTipoProducto() As String
        Get
            Return lDrTipoProd.IDTIPOPRODUCTO
        End Get

    End Property

    Public ReadOnly Property Descripcion() As String
        Get
            Return lDrTipoProd.DESCRIPCION
        End Get
    End Property

    Public ReadOnly Property Prefijo() As String
        Get
            Return lDrTipoProd.PREFIJO
        End Get
    End Property
    Protected Overrides Sub Finalize()
        lDrTipoProd = Nothing
        ldtTipoProd = Nothing
        lDaTipoProd = Nothing
    End Sub
End Class

Public Class cProducto

    'Private lDtProducto As AdminVentasDataSet.tbProductosDataTable
    Private lTipoProducto As cTipoProducto
    Private lTipoIva As cTipoIva
    Private lDrProducto As lbGlobalcom.dsProductos.PRO_PRODUCTORow
    Private lEsta As Boolean = False
    Sub New()
        
    End Sub

    Sub New(ByVal IdProducto As String)
        Try
            Dim daProd As New dsProductosTableAdapters.PRO_PRODUCTOTableAdapter
            Dim dtProd As lbGlobalcom.dsProductos.PRO_PRODUCTODataTable = daProd.GetDataByidProducto(IdProducto)
            If dtProd.Rows.Count > 0 Then
                lDrProducto = dtProd.Rows(0)
                lTipoProducto = New cTipoProducto(lDrProducto.IDTIPOPRODUCTO)
                lTipoIva = New cTipoIva(lDrProducto.IDTIPOIVA)
                lEsta = True
            Else
                lEsta = False
                Throw New Exception("Producto no existe, favor verifique e intente de nuevo")
            End If
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        End Try

    End Sub
    Public ReadOnly Property ArticuloExiste() As Boolean
        Get
            Return lEsta
        End Get
    End Property
    Public Shared Function GetListaProductos() As DataSet
        Try
            Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
                Using cmd As OracleCommand = conn.CreateCommand
                    cmd.CommandText = "SELECT * FROM PRO_PRODUCTO"
                    Dim dsProd As New DataSet
                    Dim dtProd As New DataTable
                    conn.Open()
                    dtProd.Load(cmd.ExecuteReader)
                    conn.Close()
                    dsProd.Tables.Add(dtProd)
                    Return dsProd
                End Using
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Property idProducto() As String
        Get
            Return lDrProducto.IDPRODUCTO
        End Get
        Set(ByVal value As String)
            lDrProducto.IDPRODUCTO = value
        End Set
    End Property
    Public ReadOnly Property IdProdConvierte() As String
        Get
            Return cGeneral.NToCad(lDrProducto("IDPRODCONVIERTE"))
        End Get
    End Property
    Public Property Descripcion() As String
        Get
            Return lDrProducto.DESCRICION
        End Get
        Set(ByVal value As String)
            lDrProducto.DESCRICION = value
        End Set
    End Property

    Public Property Unidad() As String
        Get
            Return lDrProducto.UNIDAD
        End Get
        Set(ByVal value As String)
            lDrProducto.UNIDAD = value
        End Set
    End Property

    Public Property Contenido() As Integer
        Get
            Return lDrProducto.CONTENIDO
        End Get
        Set(ByVal value As Integer)
            lDrProducto.CONTENIDO = value
        End Set
    End Property

    Public Property Referencia() As String
        Get
            Return lDrProducto.REFERENCIA
        End Get
        Set(ByVal value As String)
            lDrProducto.REFERENCIA = value
        End Set
    End Property

    Public Property EAN() As String
        Get
            Return lDrProducto.EAN
        End Get
        Set(ByVal value As String)
            lDrProducto.EAN = value
        End Set
    End Property

    Public ReadOnly Property iTipoProducto() As cTipoProducto
        Get
            Return lTipoProducto
        End Get
    End Property

    Public ReadOnly Property iTipoIva() As cTipoIva
        Get
            Return lTipoIva
        End Get
    End Property

    Public Property Costo() As Integer
        Get
            Return lDrProducto.COSTO
        End Get
        Set(ByVal value As Integer)
            lDrProducto.COSTO = value
        End Set
    End Property

    Public Property IdTipoIva() As String
        Get
            Return lDrProducto.IDTIPOIVA
        End Get
        Set(ByVal value As String)
            lDrProducto.IDTIPOIVA = value
            lTipoIva = New cTipoIva(lDrProducto.IDTIPOIVA)
        End Set
    End Property

    Public Property IdTipoProducto() As String
        Get
            Return lDrProducto.IDTIPOPRODUCTO
        End Get
        Set(ByVal value As String)
            lDrProducto.IDTIPOPRODUCTO = value
            lTipoProducto = New cTipoProducto(lDrProducto.IDTIPOPRODUCTO)
        End Set
    End Property

    Public Property estado() As Decimal
        Get
            Return lDrProducto.ESTADO
        End Get
        Set(ByVal value As Decimal)
            lDrProducto.ESTADO = value
        End Set
    End Property

    Public Property aplicaRangoDto() As Char
        Get
            Return lDrProducto.APLICARANGODTO
        End Get
        Set(ByVal value As Char)
            lDrProducto.APLICARANGODTO = value
        End Set
    End Property

    Public ReadOnly Property EsPadre() As String
        Get
            Return lDrProducto.ESPADRE
        End Get
    End Property

    Public ReadOnly Property TipoSubProd() As Int16
        Get
            Return lDrProducto.TIPOSUBPROD
        End Get
    End Property

    Public ReadOnly Property CantidadSubProd() As Int32
        Get
            Return lDrProducto.CANTIDADSUBPROD
        End Get
    End Property

    Public ReadOnly Property ICCIDEnCompras() As String
        Get
            Return lDrProducto.ICCIDENCOMPRAS
        End Get
    End Property

    Protected Overrides Sub Finalize()
        lTipoIva = Nothing
        lTipoProducto = Nothing
        lDrProducto = Nothing
    End Sub

    Public Function maestroProductos() As Data.DataSet
        Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = conn.CreateCommand
                Dim ds As New DataSet
                cmd.CommandText = "SELECT IDPRODUCTO,DESCRICION,'PRECIO VENTA','PRECIO COMPRA' FROM PRO_PRODUCTO"
                Try
                    conn.Open()
                    Dim dt As New DataTable
                    dt.Load(cmd.ExecuteReader)
                    ds.Tables.Add(dt)
                    Return ds
                Catch ex As Exception
                    Throw ex
                Finally
                    conn.Close()
                End Try
            End Using
        End Using
    End Function

    Public Function productoEquivalente() As String
        Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = conn.CreateCommand
                Dim dt As New DataTable
                cmd.CommandText = "SELECT IDPRODUCTO FROM PRO_PRODUCTO" & _
                                  " WHERE DESCRICION=:PDESCRIPCION AND IDMARCA=:PMARCA AND IDTIPOPRODUCTO<>:PIDTIPOPRODUCTO"
                cmd.Parameters.Add("PDESCRIPCION", OracleDbType.Varchar2).Value = lDrProducto.DESCRICION
                cmd.Parameters.Add("PMARCA", OracleDbType.Varchar2).Value = lDrProducto.IDMARCA
                cmd.Parameters.Add("PIDTIPOPRODUCTO", OracleDbType.Varchar2).Value = lDrProducto.IDTIPOPRODUCTO

                Try
                    conn.Open()
                    Dim codigo As String = cmd.ExecuteScalar
                    Return codigo
                Catch ex As Exception
                    Throw ex
                Finally
                    conn.Close()
                End Try
            End Using
        End Using
    End Function

End Class



Public Class cEncaListaPrecios
    Private lDrEncLP As lbGlobalcom.dsProductos.PRO_ENCALISTAPRECIOSRow


    Sub New(ByVal IdLista As String)
        Try
            Dim daEncLP As New lbGlobalcom.dsProductosTableAdapters.PRO_ENCALISTAPRECIOSTableAdapter
            Dim dtEncLP As lbGlobalcom.dsProductos.PRO_ENCALISTAPRECIOSDataTable = daEncLP.GetDataByIdLista(IdLista)
            If dtEncLP.Rows.Count > 0 Then
                lDrEncLP = dtEncLP.Rows(0)
            Else
                Throw New Exception("Lista de Precios no existe, favor verificar e intentar de nuevo")
            End If
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        End Try

    End Sub

    Public ReadOnly Property DetalleLista(ByVal IdProducto As String, Optional ByVal idCanal As String = "") As cDetalleListaPrecios
        Get
            Dim lDetalleLista As cDetalleListaPrecios
            If idCanal <> "" Then
                lDetalleLista = New cDetalleListaPrecios(lDrEncLP.IDLISTA, IdProducto, idCanal)
            Else
                lDetalleLista = New cDetalleListaPrecios(lDrEncLP.IDLISTA, IdProducto)
            End If
            Return lDetalleLista
        End Get
    End Property
    Public Property idLista() As String
        Get
            Return lDrEncLP.IDLISTA
        End Get
        Set(ByVal value As String)
            lDrEncLP.IDLISTA = value
        End Set
    End Property

    Public Property Descripcion() As String
        Get
            Return lDrEncLP.DESCRIPCION
        End Get
        Set(ByVal value As String)
            lDrEncLP.DESCRIPCION = value
        End Set
    End Property

    Public Property FechaIni() As DateTime
        Get
            Return lDrEncLP.FECHAINI
        End Get
        Set(ByVal value As DateTime)
            lDrEncLP.FECHAINI = value
        End Set
    End Property

    Public Property FechaFin() As Date
        Get
            Return lDrEncLP.FECHAFIN
        End Get
        Set(ByVal value As Date)
            lDrEncLP.FECHAFIN = value
        End Set
    End Property

    Public Property Estado() As Integer
        Get
            Return lDrEncLP.ESTADO
        End Get
        Set(ByVal value As Integer)
            lDrEncLP.ESTADO = value
        End Set
    End Property

    Public ReadOnly Property Vencida() As Boolean
        Get
            Return lDrEncLP.FECHAFIN > Date.Today
        End Get
    End Property

    Public ReadOnly Property listaActual() As Boolean
        Get
            Return lDrEncLP.FECHAINI >= Date.Today And lDrEncLP.FECHAFIN <= Date.Today
        End Get
    End Property
    Public Property costoPrecio() As Boolean
        Get
            Return lDrEncLP.COSTOPRECIO
        End Get
        Set(ByVal value As Boolean)
            lDrEncLP.COSTOPRECIO = value
        End Set
    End Property


    ''' <summary>
    ''' Lista General  o Lista Especifica
    ''' </summary>
    ''' <remarks>
    ''' General=Lista de la cual parten otras lista aplicando un descuento dependiendo del canal
    ''' 
    ''' Especifica=Lista ya definida
    ''' </remarks>
    Public Property tipoLista() As eTipoLista
        Get
            Return lDrEncLP.TIPOLISTA
        End Get
        Set(ByVal value As eTipoLista)
            lDrEncLP.TIPOLISTA = value
        End Set
    End Property

    Protected Overrides Sub Finalize()
        lDrEncLP = Nothing
    End Sub

End Class

Public Class cDetalleListaPrecios
    Private iLista As cEncaListaPrecios
    Private lDrDL As lbGlobalcom.dsProductos.PRO_DETALLELISTAPRECIOSRow
    Private lcProducto As cProducto
    Private lValorLegalizacion As Double = 0
    Private lPorRenta As Decimal = 0
    ''' <summary>
    ''' el tipo de lista nos define si aplicamos dto o no
    ''' </summary>
    ''' 
    Public Sub New()

    End Sub
    Public Sub New(ByVal idLista As String, ByVal idProducto As String)
        Try
            lcProducto = New cProducto(idProducto)
            iLista = New cEncaListaPrecios(idLista)
            If iLista.FechaFin > iLista.FechaIni Then
                If iLista.FechaIni < Date.Today Then
                    Throw New Exception("Lista ya vencio, favor verificar e intenter de nuevo")
                End If
            End If
            Dim daDL As New dsProductosTableAdapters.PRO_DETALLELISTAPRECIOSTableAdapter
            Dim dtDl As dsProductos.PRO_DETALLELISTAPRECIOSDataTable = daDL.GetDataByIdListaIdProductoActual(idLista, idProducto)
            If dtDl.Rows.Count > 0 Then
                lDrDL = dtDl.Rows(0)
            Else
                Throw New Exception("Producto no existe en esta lista, favor verificar e intentar de nuevo")
            End If
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        End Try
    End Sub

    Public Sub New(ByVal idLista As String, ByVal idProducto As String, ByVal idCanal As String)
        Me.New(idLista, idProducto)
        Try

            If lcProducto.aplicaRangoDto = "S" Then
                Dim daCanal As New dsFacturacionTableAdapters.FAC_CANALESTableAdapter
                Dim dtCanal As dsFacturacion.FAC_CANALESDataTable = daCanal.GetDataByIdCanal(idCanal)
                If dtCanal.Rows.Count > 0 Then
                    lPorRenta = dtCanal.Rows(0)("PORRENTA")
                    Dim iParam As cParametrizacion = New cParametrizacion("VRLEGA", "LISTAS")
                    lValorLegalizacion = CDbl(iParam.Valor)
                Else
                    Throw New Exception("Canal no existe, favor verifique e intente de nuevo")
                End If
            Else
                lPorRenta = 0
            End If
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        End Try
    End Sub

    Public ReadOnly Property iProducto() As cProducto
        Get
            Return lcProducto
        End Get
    End Property

    Public ReadOnly Property IdLista() As String
        Get
            Return lDrDL.IDLISTA
        End Get
    End Property

    Public ReadOnly Property idProducto() As String
        Get
            Return lDrDL.IDPRODUCTO
        End Get
    End Property

    Public ReadOnly Property PrecioVenta() As Double
        Get
            Return lDrDL.PRECIOVENTA
        End Get
    End Property

    Public ReadOnly Property PrecioCosto() As Double
        Get
            Return lDrDL.PRECIOCOMPRA
        End Get
    End Property
    Public ReadOnly Property UtilidadSinLegalizacion()
        Get
            Return lDrDL.PRECIOVENTA - lDrDL.PRECIOCOMPRA
        End Get
    End Property
    Public ReadOnly Property UtilidadTotal() As Double
        Get
            Return UtilidadSinLegalizacion + lValorLegalizacion
        End Get
    End Property
    Public ReadOnly Property PrecioConDto() As Double
        Get
            If iLista.tipoLista = eTipoLista.especifico Or iProducto.aplicaRangoDto = "N" Then
                Return PrecioVenta
            Else
                Dim UtilXCanal As Double = UtilidadTotal * lPorRenta
                If UtilidadSinLegalizacion >= UtilXCanal Then
                    Return PrecioVenta - UtilXCanal
                Else
                    Return PrecioCosto
                End If
            End If
            
        End Get
    End Property

    Public ReadOnly Property Iva() As Double
        Get
            Return PrecioConDto * lcProducto.iTipoIva.PorIva
        End Get
    End Property

    Public ReadOnly Property FechaCreacion() As Date
        Get
            Return lDrDL.FECHACREACION
        End Get
    End Property

    Protected Overrides Sub Finalize()
        lDrDL = Nothing
    End Sub

    Public Sub ActualizaListaPrecios(ByVal idLista As String, ByVal idProducto As String, ByVal precioventa As Double, ByVal preciocompra As Double)
        Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = conn.CreateCommand
                cmd.CommandText = "PRO_ACTUALIZALISTAPRECIOS"
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("PIDLISTA", OracleDbType.Varchar2).Value = idLista
                cmd.Parameters.Add("PIDPRODUCTO", OracleDbType.Varchar2).Value = idProducto
                cmd.Parameters.Add("PPRECIOVENTA", OracleDbType.Decimal).Value = precioventa
                cmd.Parameters.Add("PPRECIOCOMPRA", OracleDbType.Decimal).Value = preciocompra
                Try
                    conn.Open()
                    cmd.ExecuteNonQuery()
                    conn.Close()
                Catch ex As Exception
                    Throw ex
                End Try
            End Using
        End Using
    End Sub
End Class

Public Class cPlanes
    Private lDrPlanes As lbGlobalcom.dsProductos.PRO_PLANESRow
    Sub New(ByVal idPlan As String)
        Try
            Dim daPlanes As New lbGlobalcom.dsProductosTableAdapters.PRO_PLANESTableAdapter
            Dim dtPlanes As dsProductos.PRO_PLANESDataTable = daPlanes.GetDataByIdPlan(idPlan)
            If dtPlanes.Rows.Count > 0 Then
                lDrPlanes = dtPlanes.Rows(0)
            Else
                Throw New Exception("Plan no existe, favor verificar e intentar de nuevo")
            End If
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        End Try

    End Sub
    Public Property idPlan() As String
        Get
            Return lDrPlanes.IDPLAN
        End Get
        Set(ByVal value As String)
            lDrPlanes.IDPLAN = value
        End Set
    End Property

    Public Property Descripcion() As String
        Get
            Return lDrPlanes.DESCRIPCION
        End Get
        Set(ByVal value As String)
            lDrPlanes.DESCRIPCION = value
        End Set
    End Property

    Public Property ValorBase() As Double
        Get
            Return lDrPlanes.VALORBASE
        End Get
        Set(ByVal value As Double)
            lDrPlanes.VALORBASE = value
        End Set
    End Property

    Public Property ValorIva() As Double
        Get
            Return lDrPlanes.VALORIVA
        End Get
        Set(ByVal value As Double)
            lDrPlanes.VALORIVA = value
        End Set
    End Property

    Public ReadOnly Property ValorIvaIncluido() As Double
        Get
            Return lDrPlanes.VALORBASE + lDrPlanes.VALORIVA
        End Get
    End Property

    Public Property Comision() As Double
        Get
            Return lDrPlanes.COMISION
        End Get
        Set(ByVal value As Double)
            lDrPlanes.COMISION = value
        End Set
    End Property


    Protected Overrides Sub Finalize()
        lDrPlanes = Nothing
    End Sub
End Class

Public Class cHojaVida
    Private lProducto As String
    Private lBodegaDestino As String
    Private lBodegaOrigen As String
    Private lFechaDesde As String
    Private lFechaHasta As String
    Private lDsConMov As dsConsultaMovimi
    Public ReadOnly Property producto() As String
        Get
            Return lProducto
        End Get
    End Property
    Public ReadOnly Property bodegaDestino() As String
        Get
            Return lBodegaDestino
        End Get
    End Property
    Public ReadOnly Property bodegaOrigen() As String
        Get
            Return lBodegaOrigen
        End Get
    End Property
    Public ReadOnly Property FechaDesde() As String
        Get
            Return lFechaDesde
        End Get
    End Property
    Public ReadOnly Property FechaHasta() As String
        Get
            Return lFechaHasta
        End Get
    End Property
    Public Sub New(ByVal prod As String, ByVal BodOri As String, ByVal BodDes As String, ByVal fechad As String, ByVal fechah As String, ByVal dsConMovi As dsConsultaMovimi)
        lProducto = prod
        lBodegaDestino = BodDes
        lBodegaOrigen = BodOri
        lDsConMov = dsConMovi
        lFechaDesde = fechad
        lFechaHasta = fechah
    End Sub

    Public Sub hojaVida()
        Dim conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = conn.CreateCommand
        Dim entro As Integer = 0
        Dim dt As New Data.DataTable
        Try
            cmd.CommandText = "SELECT ENC.IDDOCUMENTO,ENC.NUMERO,ENC.FECHADOCUMENTO,ENC.IDBODEGADESTINO,ENC.IDBODEGAORIGEN,DET.CANTIDAD FROM MOV_MOVIMIENTO ENC,MOV_MOVIMIENTODETALLE DET "
            cmd.CommandText &= " WHERE ENC.IDDOCUMENTO=DET.IDDOCUMENTO AND ENC.NUMERO= DET.NUMERO "
            If lProducto.Length > 0 Then
                cmd.CommandText &= " AND DET.IDPRODUCTO=:PPRODUCTO"
                cmd.Parameters.Add("PPRODUCTO", OracleDbType.Varchar2).Value = lProducto
            End If
            If lFechaDesde.Length > 0 And lFechaHasta.Length > 0 Then
                cmd.CommandText &= " AND ENC.FECHADOCUMENTO BETWEEN :PFECHADESDE AND :PFECHAHASTA"
                cmd.Parameters.Add("PFECHADESDE", OracleDbType.Date).Value = CType(lFechaDesde, Date)
                cmd.Parameters.Add("PFECHAHASTA", OracleDbType.Date).Value = CType(lFechaHasta, Date)
            End If
            If lBodegaDestino.Length > 0 Then
                cmd.CommandText &= " AND ENC.IDBODEGADESTINO=:PBODEGADESTINO"
                cmd.Parameters.Add("PBODEGADESTINO", OracleDbType.Varchar2).Value = lBodegaDestino
            End If
            If lBodegaOrigen.Length > 0 Then
                cmd.CommandText &= " AND ENC.IDBODEGAORIGEN=:PBODEGAORIGEN"
                cmd.Parameters.Add(":PBODEGAORIGEN", OracleDbType.Varchar2).Value = lBodegaOrigen
            End If
            conn.Open()
            dt.Load(cmd.ExecuteReader)
            For Each dr In dt.Rows
                Dim drMov As dsConsultaMovimi.DETALLEMOVIMIENTORow
                drMov = lDsConMov.DETALLEMOVIMIENTO.NewRow
                drMov("IDDOCUMENTO") = dr("IDDOCUMENTO")
                drMov("NUMERO") = dr("NUMERO")
                drMov("FECHADOCUMENTO") = dr("FECHADOCUMENTO")
                drMov("IDBODEGADESTINO") = dr("IDBODEGADESTINO")
                drMov("IDBODEGAORIGEN") = dr("IDBODEGAORIGEN")
                drMov("CANTIDAD") = dr("CANTIDAD")
                lDsConMov.DETALLEMOVIMIENTO.Rows.Add(drMov)
            Next
            'lDsMovDtalle.MOV_MOVIMIENTODETALLE.Load(cmd.ExecuteReader)
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        Finally
            conn.Close()
        End Try
    End Sub
End Class
Public Class cGetCodigoProducto
    Private lPrefijoM As String
    Private lPrefijoTP As String
    Private lNumeroDijitos As Int16
    Private lCodigoProd As String
    Private Function GetConsec() As Integer
        Using Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = Conn.CreateCommand
                cmd.CommandText = "SELECT MAX(IDPRODUCTO) AS IDPRODUCTO FROM PRO_PRODUCTO WHERE IDPRODUCTO LIKE '" & lPrefijoM & lPrefijoTP & "%'"
                Try
                    Conn.Open()
                    Dim dt As New DataTable
                    dt.Load(cmd.ExecuteReader)
                    Conn.Close()
                    If dt.Rows.Count <> 0 AndAlso dt.Rows(0)("IDPRODUCTO") IsNot DBNull.Value Then
                        Return dt.Rows(0)("IDPRODUCTO").ToString.Substring(lPrefijoM.Trim.Length + lPrefijoTP.Trim.Length) + 1
                    Else
                        Return 1
                    End If
                Catch ex As Exception
                    cError.SalvaError(ex, "Samuel")
                    Throw ex
                End Try
            End Using
        End Using
    End Function
    Public Sub New(ByVal IdMarca As String, ByVal IdTipoProd As String)
        Dim cMar As cMarcas
        Dim cTP As cTipoProducto
        Try
            cMar = New cMarcas(IdMarca)
            cTP = New cTipoProducto(IdTipoProd)
            If cTP Is Nothing Then
                Throw New Exception("No existe Tipo Producto favor verifique e intente de nuevo")
            End If
            If cMar Is Nothing Then
                Throw New Exception("Marca no existe favor verifique e intente de nuevo")
            End If
            lPrefijoTP = cTP.Prefijo.Trim
            lPrefijoM = cMar.Prefijo.Trim
            lNumeroDijitos = cMar.Dijitos
            lCodigoProd = lPrefijoM & lPrefijoTP & GetConsec().ToString.PadLeft(lNumeroDijitos, "0")
        Catch ex As Exception
            cError.SalvaError(ex, "Samuel")
            Throw ex
        End Try
    End Sub
    Public ReadOnly Property CodigoProducto() As String
        Get
            Return lCodigoProd
        End Get
    End Property
End Class